<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id.toString()">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | date }}</span>
            <van-button size="mini" type="default" @click="showReply(item)"
              >回复 {{ item.reply_count }}</van-button
            >
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="likeComment(item)"
        />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model.trim="content">
        <van-button slot="button" size="mini" type="info" @click="add"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-popup v-model="isShowReply" position="bottom" title="评论回复">
      <van-nav-bar
        title="评论回复"
        right-text="取消"
        @click-right="isShowReply = false"
      >
      </van-nav-bar>
      <CommentReply
        @update="updateCount"
        v-if="isShowReply"
        :comment="comment"
      ></CommentReply>
    </van-popup>
  </div>
</template>

<script>
import CommentReply from './commentReply'
import {
  getCommentList,
  addComment,
  addLikeComment,
  delLikeComment
} from '@/api/comment'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      articleId: '',
      offset: null,
      content: '',
      isShowReply: false,
      comment: {}
    }
  },
  created() {
    this.articleId = this.$route.params.id
  },
  methods: {
    updateCount(id) {
      console.log(id, this.list)
      const comment = this.list.find(
        item => item.com_id.toString() === id.toString()
      )
      comment.reply_count++
    },
    async onLoad() {
      const res = await getCommentList(this.articleId, this.offset)
      const { results, last_id: offset } = res.data.data
      this.list = [...this.list, ...results]
      this.offset = offset
      this.loading = false

      if (results.length === 0) {
        this.finished = true
      }
    },
    async add() {
      if (!this.content) return
      try {
        const res = await addComment(this.articleId, this.content)
        this.$toast.success('评论成功')
        this.list.unshift(res.data.data.new_obj)

        this.content = ''
      } catch (err) {
        this.$toast('该文章关闭了评论功能')
      }
    },
    showReply(item) {
      this.isShowReply = true
      this.comment = item
    },
    async likeComment(item) {
      if (item.is_liking) {
        await delLikeComment(item.com_id.toString())
      } else {
        await addLikeComment(item.com_id.toString())
      }
      this.$toast.success('操作成功')
      item.is_liking = !item.is_liking
    }
  }
}
</script>

<style scoped lang="scss">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
